@model ListContentsViewModel
@using OrchardCore.ContentManagement;
@using OrchardCore.Contents.ViewModels;
@inject IContentManager ContentManager

@{
    var contentTypeDisplayName = Model.Options.ContentTypeOptions.Find(x => x.Value == Model.Options.SelectedContentType && x.Value != null)?.Text;
    var pageTitle = T["Manage Content"];
    var createLinkText = T["Create New Content"];
    if (!string.IsNullOrWhiteSpace(contentTypeDisplayName))
    {
        pageTitle = T["Manage {0} Content", (string)contentTypeDisplayName];
        createLinkText = T["Create New {0}", (string)contentTypeDisplayName];
    }

    var formActionWithoutTypeName = Context.Request.PathBase + "/Admin/Contents/ContentItems";
}

<h1>@RenderTitleSegments(pageTitle)</h1>
@using (Html.BeginForm("List", "Admin", FormMethod.Post, new { id = "items-form", autocomplete = "off" }))
{
    <!-- Hidden submit button do not remove -->
    <input type="submit" name="submit.Filter" id="submitFilter" class="d-none" />
    <input type="submit" name="submit.BulkAction" class="d-none" />
    <input asp-for="Options.BulkAction" type="hidden" />

    <fieldset class="filter-options">
        <div class="card mb-3">
            <div class="card-body bg-light p-3">
                <div class="row">
                    <div class="form-group col-xl-3 mb-xl-n1">
                        <div class="input-group input-group-sm">
                            <div class="input-group-prepend">
                                <span class="input-group-text bg-secondary text-light">@T["Title"]</span>
                            </div>
                            <input asp-for="Options.DisplayText" class="form-control" />
                        </div>
                    </div>
                    <div class="form-group col-xl-3 mb-xl-n1">
                        <div class="input-group input-group-sm">
                            <div class="input-group-prepend">
                                <span class="input-group-text bg-secondary text-light">@T["Filter"]</span>
                            </div>
                            <select asp-for="Options.SelectedContentType" class="form-control" asp-items="Model.Options.ContentTypeOptions"></select>
                        </div>
                    </div>

                    @*@if (cultures.Count() > 1) {
                        <div class="col-md-2 col-xl-1 form-group">
                            <label for="filterCultures" class="label col-form-label">@T["Culture"]</label>
                        </div>
                        <div class="col-md-4 col-xl-3 form-group">
                            <select id="filterCultures" class="form-control" name="Options.SelectedCulture">
                                <option value="">@T["All cultures"]</option>
                                @foreach (string culture in cultures) {
                                    string isSelected = culture == Model.Options.SelectedCulture ? "selected='selected'" : string.Empty;
                                    <!option @isSelected value="culture">@System.Globalization.CultureInfo.GetCultureInfo(culture).DisplayName</!option>
                                }
                            </select>
                        </div>
                        }*@
                    <div class="form-group col-xl-3 mb-xl-n1">
                        <div class="input-group input-group-sm">
                            <div class="input-group-prepend">
                                <span class="input-group-text bg-secondary text-light">@T["Show"]</span>
                            </div>
                            <select asp-for="Options.ContentsStatus" class="form-control" asp-items="@Model.Options.ContentStatuses"></select>
                        </div>
                    </div>
                    <div class="form-group col-xl-3 mb-n1">
                        <div class="input-group input-group-sm">
                            <div class="input-group-prepend">
                                <span class="input-group-text bg-secondary text-light">@T["Sort by"]</span>
                            </div>
                            <select asp-for="Options.OrderBy" class="form-control" asp-items="@Model.Options.ContentSorts"></select>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </fieldset>
    <div class="row">
        <div class="form-group col-6 float-left">
            <div class="input-group">
                <div class="input-group-text">
                    <div class="custom-control custom-checkbox mr-n2">
                        <input type="checkbox" class="custom-control-input" id="select-all">
                        <label class="custom-control-label" for="select-all"></label>
                    </div>
                </div>
                <div class="input-group-append">
                    <span class="input-group-text bg-secondary text-light">@T["Select All"]</span>
                </div>
            </div>
        </div>
        <div class="form-group col-6 float-right">
            <div class="dropdown float-right">
                <button disabled class="btn btn-secondary dropdown-toggle" type="button" id="bulk-action-menu-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    @T["Bulk Actions"]
                </button>
                <div class="dropdown-menu dropdown-menu-right" aria-labelledby="bulk-action-menu-button">
                    @foreach (var item in Model.Options.ContentsBulkAction)
                    {
                        <a class="dropdown-item" href="javascript:void(0)" data-action="@item.Value" data-title="@T["Bulk Action"]" data-message="@T["Are you sure you want to {0} this/these item(s)?", @item.Text.ToLower()]">@item.Text</a>
                    }
                </div>
            </div>
        </div>
    </div>
    @if (Model.ContentItems.Count > 0)
    {
        <ul class="list-group">
            @foreach (var contentItemSummary in Model.ContentItems)
            {
                <li class="list-group-item">
                    @await DisplayAsync(contentItemSummary)
                </li>
            }
        </ul>
    }
    else
    {
        <div class="alert alert-info">
            @T["No result found."]
        </div>
    }

    @await DisplayAsync(Model.Pager)
}

@* This is because ContentTypesAdminNode requires the ContenType to be available as part of the url *@
<script at="Foot" type="text/javascript">
    $(function () {
        var bulkActionButton = $("#bulk-action-menu-button");
        var selectAllCtrl = $("#select-all");
        var contentItemsCheckboxes = $(":checkbox[name='itemIds']");

        $("#Options_SelectedContentType").change(function (e) {
            var value = $("#Options_SelectedContentType option:selected").val();
            console.log(value);
            var newAction = "@formActionWithoutTypeName" + "/" + value;
            $("#items-form").attr("action", newAction);
        });

        $(".filter-options select, .filter-options input").on("change", function () {
            $("[name='submit.Filter']").click();
        });

        $(".dropdown-menu .dropdown-item").on("click", function () {
            if ($(":checkbox[name='itemIds']:checked").length > 1) {
                var $this = $(this);
                confirmDialog({title: $this.data('title'), message: $this.data('message'), callback: function(r) {
                    if (r) {
                        $("[name='Options.BulkAction']").val($this.data("action"));
                        $("[name='submit.BulkAction']").click();
                    }
                }});
            }
        });

        selectAllCtrl.click(function(){
            contentItemsCheckboxes.not(this).prop("checked", this.checked);
            bulkActionButton.attr("disabled", !this.checked);
        });

        contentItemsCheckboxes.on("click", function () {
            bulkActionButton.attr("disabled", !($(":checkbox[name='itemIds']:checked").length > 1));
        });
    })
</script>
